package cn.hxzy.icon.icon.controller;

import cn.hxzy.icon.icon.entity.Image;
import cn.hxzy.icon.icon.entity.po.ImagePo;
import cn.hxzy.icon.icon.entity.vo.PageBean;
import cn.hxzy.icon.icon.entity.vo.ReturnBean;
import cn.hxzy.icon.icon.service.ImageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("image")
public class ImageController {
    @Autowired
    private ImageService imageService;

    /**
     * 显示全部
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping("list")
    public ReturnBean list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "8") Integer size) {
        PageBean<Image> all = imageService.findAll(new PageBean<Image>(page, size));
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @PostMapping("findById")
    public ReturnBean findById(Integer id) {
        Image image=imageService.findById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, image);
    }

    /**
     * 添加
     *
     * @param name
     * @param file
     * @return
     */
    @PostMapping("add")
    public ReturnBean add(String name, MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String fileName = file.getOriginalFilename();
            String path = "G:/resource/zy_icon/img/";
            String type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".")) : null;
            File file1 = new File(path + uuid + type);
            file.transferTo(file1);
            Image image = new Image();
            image.setName(name);
            image.setUrl("/img/" + uuid+type);
            imageService.add(image);
        }

        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    /**
     * 修改
     * @param name
     * @param file
     * @param imagePo
     * @param id
     * @return
     * @throws IOException
     */
    @PostMapping("update")
    public ReturnBean update(String name, MultipartFile file, ImagePo imagePo,Integer id) throws IOException {
        if (!file.isEmpty()) {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String fileName = file.getOriginalFilename();
            String path = "G:/resource/zy_icon/img/";
            String type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".")) : null;
            File file1 = new File(path + uuid + type);
            file.transferTo(file1);
            Image image = new Image();
            imagePo.setName(name);
            imagePo.setId(id);
            imagePo.setUrl("/img/" + uuid+type);
            BeanUtils.copyProperties(imagePo, image);
            imageService.update(image);
            return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
        }
        return ReturnBean.of(ReturnBean.AnswerCode.OBJECT_NOT_EXIST);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("deleteId")
    public ReturnBean deleteId(Integer id) {
        imageService.deleteById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
}
